home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #11 / Amiga Plus CD - 2004 - No. 11.iso / AmiSoft / Util / conv / Acvt.lha / Acvt 1.07 / sources / cdisk.cpp < prev    next >
C/C++ Source or Header  |  1999-06-10  |  6KB  |  277 lines

  1. //    This program is free software; you can redistribute it and/or modify
  2. //    it under the terms of the GNU General Public License as published by
  3. //    the Free Software Foundation; either version 2 of the License, or
  4. //    any later version.
  5. //
  6. //    This program is distributed in the hope that it will be useful,
  7. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9. //    GNU General Public License for more details.
  10. //
  11. //    You should have received a copy of the GNU General Public License
  12. //    along with this program; if not, write to the Free Software
  13. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. //
  15.  
  16. #include "cdisk.h"
  17. #include "cdsk_atr.h"
  18. #include "cdsk_xfd.h"
  19. #include "cdsk_dcm.h"
  20. #include "cdsk_scp.h"
  21.  
  22.  
  23. CDisk::CDisk()
  24.     m_bOpened = FALSE; 
  25.     m_pbtMemory = NULL;
  26.  
  27.     #ifdef _MEMORY_DUMP_
  28.         printf( "CDisk constructed: %08X\n", this );
  29.     #endif
  30. }
  31.  
  32. CDisk::~CDisk()
  33. {
  34.     if ( m_pbtMemory )
  35.     {
  36.         delete [] m_pbtMemory;
  37.         m_pbtMemory = NULL;
  38.     }
  39.  
  40.     #ifdef _MEMORY_DUMP_
  41.         printf( "CDisk destructed: %08X\n", this );
  42.     #endif
  43. }
  44.  
  45. BOOL CDisk::Duplicate( CDisk* pDisk )
  46. {
  47.     if ( !Format( & ( pDisk->m_geometry ) ) )
  48.         return FALSE;
  49.  
  50.     memcpy( m_pbtMemory, pDisk->m_pbtMemory, pDisk->m_iAllocated );
  51.     return TRUE;
  52. }
  53.  
  54. BOOL CDisk::Format( DISK_GEOMETRY* pgeo )
  55. {
  56.     int iSectors = pgeo->iSides * pgeo->iTracks * pgeo->iSectorsPerTrack;
  57.  
  58.     if ( m_pbtMemory )
  59.         delete [] m_pbtMemory;
  60.  
  61.     m_iAllocated = iSectors * pgeo->iBytesPerSector;
  62.     m_pbtMemory = new BYTE [ m_iAllocated ];
  63.  
  64.     if ( !m_pbtMemory )
  65.     {
  66.         sprintf( m_szLastError, "DISK: Can't format - Not enough memory!" );
  67.         return FALSE;
  68.     }
  69.  
  70.     memset( m_pbtMemory, 0, m_iAllocated );
  71.  
  72.     m_geometry.iSides = pgeo->iSides;
  73.     m_geometry.iTracks = pgeo->iTracks;
  74.     m_geometry.iSectorsPerTrack = pgeo->iSectorsPerTrack;
  75.     m_geometry.iBytesPerSector = pgeo->iBytesPerSector;
  76.     m_geometry.iSectors = iSectors;
  77.  
  78.     return TRUE;
  79. }
  80.  
  81. BOOL CDisk::ReadSector( void* pBuf, int iStartSec )
  82. {
  83.     if ( iStartSec && ( iStartSec <= m_geometry.iSectors ) )
  84.         memcpy( pBuf, m_pbtMemory + ( iStartSec - 1 ) * m_geometry.iBytesPerSector, m_geometry.iBytesPerSector );
  85.     else
  86.     {
  87.         sprintf( m_szLastError, "DISK: Reading non-existent sector: %04X", iStartSec );
  88.         return FALSE;
  89.     }
  90.  
  91.     return TRUE;
  92. }
  93.  
  94. BOOL CDisk::ReadSectors( void* pBuf, int iStartSec, int iSecs )
  95. {
  96.     while( iSecs )
  97.     {
  98.         if ( !ReadSector( pBuf, iStartSec ) )
  99.             return FALSE;
  100.  
  101.         iStartSec++;
  102.         iSecs--;
  103.         pBuf = (BYTE*)pBuf + m_geometry.iBytesPerSector;
  104.     }
  105.  
  106.     return TRUE;
  107. }
  108.  
  109. BOOL CDisk::WriteSector( int iStartSec, void* pBuf )
  110. {
  111.     //printf( " ws: %d ", iStartSec );
  112.     if ( iStartSec && ( iStartSec <= m_geometry.iSectors ) )
  113.         memcpy( m_pbtMemory + ( iStartSec - 1 ) * m_geometry.iBytesPerSector, pBuf, m_geometry.iBytesPerSector );
  114.     else
  115.     {
  116.         sprintf( m_szLastError, "DISK: Writing non-existent sector: %04X", iStartSec );
  117.         return FALSE;
  118.     }
  119.  
  120.     return TRUE;
  121. }
  122.  
  123. void GuessClassicSizes( int iSectors, int iSectorSize, DISK_GEOMETRY* pGeometry )
  124. {
  125.     pGeometry->iSides = 1;
  126.     pGeometry->iBytesPerSector = iSectorSize;
  127.  
  128.     pGeometry->iTracks = 1;
  129.     pGeometry->iSectorsPerTrack = iSectors;
  130.  
  131.     switch( iSectors )
  132.     {
  133.         case 720:
  134.             switch( iSectorSize )
  135.             {
  136.                 case 0x80:
  137.                     pGeometry->iTracks = 40;
  138.                     pGeometry->iSectorsPerTrack = 18;
  139.                     break;
  140.  
  141.                 case 0x100:
  142.                     pGeometry->iTracks = 40;
  143.                     pGeometry->iSectorsPerTrack = 18;
  144.                     break;
  145.             }
  146.             break;
  147.  
  148.         case 1040:
  149.             pGeometry->iTracks = 40;
  150.             pGeometry->iSectorsPerTrack = 26;
  151.             break;
  152.  
  153.     }
  154.  
  155. }
  156.  
  157. //returns ptr to disk type name
  158. char* GetDiskTypeName( DISK_TYPE disktype )
  159. {
  160.     switch( disktype )
  161.     {
  162.         case DISK_XFD:
  163.             return "XFD";
  164.  
  165.         case DISK_ATR:
  166.             return "ATR";
  167.  
  168.         case DISK_XFDb:
  169.             return "XFDb";
  170.  
  171.         case DISK_ATRb:
  172.             return "ATRb";
  173.  
  174.         case DISK_DCM:
  175.             return "DCM";
  176.  
  177.         case DISK_SCP:
  178.             return "SCP";
  179.  
  180.         default:
  181.             return "None";
  182.     }
  183. }
  184.  
  185. //returns ptr to disk type extension
  186. char* GetDiskTypeExt( DISK_TYPE disktype )
  187. {
  188.     switch( disktype )
  189.     {
  190.         case DISK_XFD:
  191.         case DISK_XFDb:
  192.             return "xfd";
  193.  
  194.         case DISK_ATR:
  195.         case DISK_ATRb:
  196.             return "atr";
  197.  
  198.         case DISK_DCM:
  199.             return "dcm";
  200.  
  201.         case DISK_SCP:
  202.             return "scp";
  203.  
  204.         default:
  205.             return "xxx";
  206.     }
  207. }
  208.  
  209. DISKINIT_RETCODE InitializeDisk( CDisk** ppDisk, DISK_TYPE disktype, char* szFname, BOOL bVerbose, BOOL bRepair, BOOL bRepairAuto )
  210. {
  211.     switch( disktype )
  212.     {
  213.         case DISK_ATR:
  214.             *ppDisk = new CAtr();
  215.             break;
  216.  
  217.         case DISK_SCP:
  218.             *ppDisk = new CScp();
  219.             break;
  220.  
  221.         case DISK_DCM:
  222.             *ppDisk = new CDcm();
  223.             break;
  224.  
  225.         case DISK_XFD:
  226.             *ppDisk = new CXfd();
  227.             break;
  228.  
  229.         default:
  230.             if ( bVerbose )
  231.                 fprintf( stderr, "Invalid disk type specified!\n" );
  232.             return DI_RET_CANT_CONTINUE;
  233.     }
  234.  
  235.     if ( !*ppDisk )
  236.     {
  237.         if ( bVerbose )
  238.              fprintf( stderr, "Can't initialize disk driver!\n" );
  239.          return DI_RET_CONTINUE;
  240.     }
  241.  
  242.     if ( !(*ppDisk)->Load( szFname, bRepair, bRepairAuto ) )
  243.     {
  244.         int iError = (*ppDisk)->GetErrorCode();
  245.  
  246.         DISKINIT_RETCODE ret = DI_RET_CONTINUE;
  247.  
  248.         switch( iError )
  249.         {
  250.             case CATR_FORMAT_VIOLATED:
  251.             case CXFD_FORMAT_VIOLATED:
  252.             case CDISK_ERROR_CANT_OPEN:
  253.             case CDCM_FORMAT_VIOLATED:
  254.             case CSCP_FORMAT_VIOLATED:
  255.                 ret = DI_RET_CANT_CONTINUE;
  256.                 break;
  257.         }
  258.  
  259.         if ( bVerbose || ( ret == DI_RET_CANT_CONTINUE ) )
  260.         {
  261.             printf( "Input file '%s' ", szFname );
  262.             printf( "(%s)\n", GetDiskTypeName( disktype ) );
  263.             printf( "Load failed because:\n%s\n", (*ppDisk)->GetLastError() );
  264.         }
  265.         if ( ret != DI_RET_OK )
  266.         {
  267.             delete *ppDisk;
  268.             return ret;
  269.         }
  270.     }
  271.  
  272.     return DI_RET_OK;
  273. }
  274.  
  275.  
  276.